/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.bricket.web.common;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.bricket.domain.DomainObject;
import com.googlecode.bricket.repository.GenericDomainService;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public class DomainObjectModel<T extends DomainObject> extends
		LoadableDetachableModel<T> {
	@SpringBean
	private GenericDomainService genericDomainService;

	private final Class<T> type;
	private final Long id;

	public DomainObjectModel(Class<T> type, Long id) {
		InjectorHolder.getInjector().inject(this);
		this.type = type;
		this.id = id;
	}

	@SuppressWarnings("unchecked")
	public DomainObjectModel(T domainObject) {
		super(domainObject);
		InjectorHolder.getInjector().inject(this);
		this.type = (Class<T>) domainObject.getClass();
		this.id = domainObject.getId();
	}

	@Override
	protected T load() {
		return genericDomainService.load(type, id);
	}
}
